package com.sk.weichat.ui.groupchat;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.sk.weichat.R;
import com.sk.weichat.broadcast.MucgroupUpdateUtil;
import com.sk.weichat.helper.DialogHelper;
import com.sk.weichat.ui.base.BaseActivity;
import com.sk.weichat.ui.message.multi.NoticeListActivity;
import com.sk.weichat.ui.message.multi.RoomInfoActivity;
import com.sk.weichat.util.AppUtils;
import com.sk.weichat.util.ToastUtil;
import com.sk.weichat.view.MaxTextTwoLengthFilter;
import com.xuan.xuanhttplibrary.okhttp.HttpUtils;
import com.xuan.xuanhttplibrary.okhttp.callback.BaseCallback;
import com.xuan.xuanhttplibrary.okhttp.result.ObjectResult;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

/**
 * 群组 名称 昵称 公共、描述 内容等修改
 */
public class RoomChangeContentActivity extends BaseActivity implements View.OnClickListener {

    public static final int CHANGE_RESULT = 0;
    public static final int USERNAME = 1;//名称
    public static final int NICKNAME = 2;//昵称
    public static final int DESCRIBE = 3;//描述
    public static final int NOTICE = 4;//公告
    public static final int REMARKNAME = 5;//对群内成员备注

    private TextView mTitleTv;
    private TextView tv_title_right;
    private EditText edit_input_content;
    private TextView tv_input_length;

    private String content;
    private int change_type;

    private int height = 0;//输入框高度
    private int maxLen = 0;//输入内容最大字符数

    private boolean isRooms = false;//判断是否是群管理页进入
    int mRole;
    String mRoomId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_room_change_content);
        initActionBar();
        initView();
    }

    private void initActionBar() {
        getSupportActionBar().hide();

        change_type = getIntent().getIntExtra("change_type", 0);//修改类型
        content = getIntent().getStringExtra("content");
        isRooms = getIntent().getBooleanExtra("isRooms", false);
        if (isRooms) {
            mRole = getIntent().getIntExtra("mRole", 0);
            mRoomId = getIntent().getStringExtra("mRoomId");
        }
        findViewById(R.id.iv_title_left).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setResult(CHANGE_RESULT);
                finish();
            }
        });
        mTitleTv = findViewById(R.id.tv_title_center);
        tv_title_right = findViewById(R.id.tv_title_right);
        tv_title_right.setText("保存");
        tv_title_right.setEnabled(false);
        tv_title_right.setOnClickListener(this);
    }

    private void initView() {

        edit_input_content = findViewById(R.id.edit_input_content);
        tv_input_length = findViewById(R.id.tv_input_length);
        if (change_type == USERNAME) {//修改群名称
            height = 120;
            maxLen = 24;
            mTitleTv.setText("群聊名称");
            edit_input_content.setHint("请输入群聊名称");
        } else if (change_type == NICKNAME) {
            height = 120;
            maxLen = 24;
            mTitleTv.setText("群内昵称");
            edit_input_content.setHint("请输入群聊昵称");

        } else if (change_type == DESCRIBE) {
            height = 450;
            maxLen = 300;
            mTitleTv.setText("群聊介绍");
            edit_input_content.setHint("请输入群聊介绍");

        } else if (change_type == NOTICE) {
            height = 478;
            maxLen = 600;
            tv_title_right.setText("发布");
            mTitleTv.setText("群内公告");
            edit_input_content.setHint("请输入群内公告");
        } else if (change_type == REMARKNAME) {
            height = 120;
            maxLen = 24;
            mTitleTv.setText("修改备注名");
            edit_input_content.setHint("请输入备注名");
        }
        ViewGroup.LayoutParams lp = edit_input_content.getLayoutParams();
        lp.height = height;
        edit_input_content.setLayoutParams(lp);
        edit_input_content.addTextChangedListener(new MyTextWatcher());
        edit_input_content.setText(content);
        edit_input_content.setSelection(content.length());
        edit_input_content.setFilters(new InputFilter[]{new MaxTextTwoLengthFilter(maxLen)});

        //打开软键盘
//        AppUtils.openKeyboard(this);
    }

    @Override
    public void onClick(View v) {
        if (v == tv_title_right) {
            if (TextUtils.isEmpty(edit_input_content.getText().toString())) {
                ToastUtil.showToast(this, "请输入内容");
            } else {
                if (isRooms) {//如果是群公告修改
                    updateNotice(edit_input_content.getText().toString());
                } else {
                    Intent intent = new Intent();
                    intent.putExtra("content", edit_input_content.getText().toString());
                    intent.putExtra("change_type", change_type);
                    setResult(CHANGE_RESULT, intent);
                }
                finish();
            }
        }
    }

    @Override
    public void onBackPressed() {
        if (!isRooms) {
            setResult(CHANGE_RESULT);
        }
        finish();
    }

    private class MyTextWatcher implements TextWatcher {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (s.length() == 0) {
                tv_title_right.setTextColor(getResources().getColor(R.color.color_FFBDC2C8));
                tv_title_right.setEnabled(false);
                tv_input_length.setText("0/" + maxLen);
            } else {
                if (!s.equals(content)) {
                    tv_title_right.setTextColor(getResources().getColor(R.color.deep_purple));
                    tv_title_right.setEnabled(true);
                }
                tv_input_length.setText(AppUtils.judgeTextLength(s.toString()) + "/" + maxLen);
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    }

    private void updateNotice(final String text) {
        Map<String, String> params = new HashMap<>();
        params.put("access_token", coreManager.getSelfStatus().accessToken);
        params.put("roomId", mRoomId);
        params.put("notice", text);
        DialogHelper.showDefaulteMessageProgressDialog(this);

        HttpUtils.get().url(coreManager.getConfig().ROOM_UPDATE)
                .params(params)
                .build()
                .execute(new BaseCallback<Void>(Void.class) {
                    @Override
                    public void onResponse(ObjectResult<Void> result) {
                        DialogHelper.dismissProgressDialog();
                        if (result.getResultCode() == 1) {
                            ToastUtil.showToast(RoomChangeContentActivity.this, "发布成功");
                            // 更新群管理界面
                            MucgroupUpdateUtil.broadcastUpdatGroupInfoeUi(RoomChangeContentActivity.this);
                            Intent intent = new Intent(RoomChangeContentActivity.this, NoticeListActivity.class);
                            intent.putExtra("mRole", mRole);
                            intent.putExtra("mRoomId", mRoomId);
                            startActivity(intent);
                            finish();
                        }
                    }

                    @Override
                    public void onError(Call call, Exception e) {
                        DialogHelper.dismissProgressDialog();
                        ToastUtil.showErrorNet(mContext);
                    }
                });
    }
}
